<?php

class IndexEightModel extends Model
{
    /**
     * 使用的表名称.
     *
     * @var string
     */
    protected $tableName = 'index_eight';

    /**
     * 字段列表.
     *
     * @var unknown_type
     */
    protected $fields = array(
        0          => 'id',
        1          => 'picture',
        2          => 'show_link',
        3          => 'sort',
        4          => 'state',
        5          => 'is_audit',
        6          => 'add_time',
        7          => 'update_time',
        8          => 'title',
        '_autoinc' => true,
        '_pk'      => 'id',
    );



    /**
     * 获取状态
     *
     * @param int $state 状态ID
     *
     * @return mixed
     */
    public function getState($state = null)
    {
        $states = array(
            1 => '显示',
            0 => '不显示',
        );
        if ($state === null) {
            return $states;
        }

        return isset($states[$state]) ? $states[$state] : '';
    }


    /**
     * 前台列表获取.
     */
    public function getList($limit = 20, $order = '', $findPage = true)
    {
        $map = array();
        $map['state'] = array('GT', 0);

        if ($order == '') {
            $order = 'sort desc,id desc';
        }

        $this->where($map)->order($order);
        if ($findPage) {
            $list = $this->findPage($limit);
            $data = $list['data'];
        } else {
            $data = $this->findAll(array('limit' => $limit));
        }
        foreach ($data as $k => $v) {
            $thumb = APPS_URL.'/'.APP_NAME.'/_static/nopic.jpg';

        }
        if ($findPage) {
            $list['data'] = $data;

            return $list;
        } else {
            return $data;
        }
    }

    /**
     * 根据ID获取资料.
     *
     * @param int  $id
     * @param bool $is_admin 是否是后台
     *
     * @return array
     */
    public function getOneyById($id, $is_admin = false, $update_hits = false)
    {
        $map = array(
            'design_id' => $id,
        );
        if ($is_admin == false) {
            $map['state'] = array('GT', 0);
        }
        $v = $this->where($map)->find();
        if ($v) {
            $v['attachId'] = $v['picture'];
            $thumb = APPS_URL.'/'.APP_NAME.'/_static/nopic.jpg';
            if ($v['image']) {
                $attach = model('Attach')->getAttachById($v['picture']);
                if ($attach) {
                    $thumb = getImageUrl($attach['save_path'].$attach['save_name'], 100, 100);
                }
            }
            $v['picture'] = $thumb;
            //更新浏览量
            if ($is_admin === false && $update_hits == true) {
                $this->setInc('hits', array('design_id' => $id), 1);
            }
        }

        return $v;
    }

    /**
     * 获取子分类的ID串.
     *
     * @param int $pid
     *
     * @return array
     */
    private function getChildTids($pid)
    {
        static $_result = array();
        if (isset($_result[$pid])) {
            return $_result[$pid];
        }
        //查找
        $child = model('CategoryTree')->setTable('news_category')->getNetworkList($pid);
        $ids = array_keys($child);
        $_result[$pid] = ($ids) ? $ids : array();

        return $ids;
    }

    /**
     * 获取信息.
     *
     * @param int $id
     *
     * @return mixed
     */
    public function getSourceInfo($id)
    {
        $find = $this->getOneyById($id);
        if ($find) {
            $info = array('source_user_info' => '');
            if ($find['uid']) {
                $info['source_user_info'] = model('User')->getUserInfo($find['uid']);
            }
            $info['source_url'] = U('news/Index/detail', array('id' => $id));
            $info['source_body'] = $find['news_title'].'<a class="ico-details" href="'.U('news/Index/show', array('id' => $id)).'"></a>';

            return $info;
        }

        return false;
    }

    /**
     * 删除分类后的回调.
     *
     * @param int $cid
     *
     * @return bool
     */
    public function deleteAssociatedData($cid)
    {
        return $this->where(array('type_id' => $cid))->delete();
    }
}
